# 装饰器(实际就是闭包）
# 不改变原有代码情况下添加新功能
# 假设 有个登陆功能的需求,但是像在不改变原soucre的情况下 添加注册功能

# 普通实现
# def funa():
#     print('login')
#     funb() 改变了原source 添加了注册功能
#
# def funb():
#     print('create new account')

# 装饰器实现
# def func():
#     print('login')

# def outer(fn):
#     def inner():
#         fn()
#         print('create new account')
#     return inner
# ot = outer(func)
# ot()

# 语法糖实现
def outer(fn):
    def inner(name):
        print('login',name)
        fn()
    return inner

@outer
def fun2():
    print('register')
fun2('123')


# 被装饰的函数有参数
# @outer
# def func(name):
#     print('register')
#
# func('Hello')

# def func(name):
#     print('register')
#
# ot = outer(func)
# ot("hello")

# def outer(fn):
#     def inner(*args,**kwargs):
#         print('login')
#         fn(*args,**kwargs)
#     return inner
# # @outer
# def f(*args,**kwargs):
#     print(args)
#     print(kwargs)

# ot = outer(f)
# ot('susu','susu',name='susu',age=1)
# f('susu','susu',name='hello',age=18)

#多个装饰器
def deco1(fn):
    def inner():
        return "deco1"+fn()+"deco1"
    return inner

def deco2(fn):
    def inner():
        return "deco2"+fn()
    return inner

@deco1
@deco2
def func():
    return 'hello'

print(func())